<?
include('custom/metatabs.php');

function displayTopTen($field, $unitCaption)
{
  global $ctrObj;
  ?><table><?
  $op = '> 0';
  $dfield = $field;
  if (substr($field, 0, 1) == ':')
  {
    $vgl = substr($field, 1);
    $field = CutSegment('=', $vgl);
    $op = ' = '.$vgl;
    $dfield = 'c_rank_xp_earned';
  }
  $list = DB_GetList('SELECT * FROM '.getTableName('characters').'
      WHERE c_ref_user != 0 AND c_rank_xp_earned > 15 AND '.$field.' '.$op.'
      ORDER BY '.$field.' DESC,c_rank_xp_earned DESC
      LIMIT 10');
  foreach($list as $chr)
  {
    if ($field == 'c_rank_victories+c_rank_defeats') 
    { 
       $chr[$dfield] = $ctrObj->l10n('alignment.'.getKillRating($chr), 'cq');
    }
    if ($unitCaption == '%') $chr[$field] = number_format($chr[$field]*100, 2);
    ?><tr><td nowrap width="40"><div align="right"><?= $chr[$dfield].' '.$unitCaption ?></div></td>
    <td valign="top"><a href="<?= actionUrl('view', 'character', 'id='.$chr['c_key']) ?>"><?= $chr['c_name'] ?></a></td></tr><?
  }
  ?></table><?
}

?>
<br/>
<table width="100%">
  <tr>
    <td valign="top" width="23%"><h4>Won Most Duels</h4>
    <?
    DisplayTopTen('c_rank_duelv', 'x');
    ?>
    </td>
    <td valign="top" width="23%"><h4>Most Experienced</h4>
    <?
    DisplayTopTen('c_rank_xp_earned', 'XP');
    ?>
    </td>
    <td valign="top" width="23%"><h4>Richest</h4>
    <?
    DisplayTopTen('c_rank_gold', 'GP');
    ?>
    </td>
  </tr><tr>
    <td valign="top" width="23%"><h4>Most Victorious</h4>
    <?
    DisplayTopTen('c_rank_vdratio', '%');
    ?>
    </td>
    <td valign="top" width="23%"><h4>Party Members</h4>
    <?
    DisplayTopTen('c_rank_party', '');
    ?>
    </td>
    <td valign="top" width="23%"><h4>Worst Alignment</h4>
    <?
    DisplayTopTen('c_rank_victories+c_rank_defeats', '');
    ?>
    </td>
  </tr>
  <tr>
    <td valign="top" width="23%"><h4>Endgame</h4>
    People who have reached the current end of content:
    <?
    DisplayTopTen(':c_location=20', 'XP');
    ?>
    </td>  
  </tr>
</table>
